package com.fd.wpx.authorizating.service.impl;

import com.fd.wpx.authorizating.service.SysUserService;
import com.fd.wpx.blog.mapper.SysUserMapper;
import com.fd.wpx.blog.pojo.SysUser;
import com.fd.wpx.blog.pojo.SysUserExample;
import com.fd.wpx.common.pojo.BlogResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    public SysUserMapper mapper;
    @Override
    public BlogResult addUser(SysUser user) {
        //添加用户，同时返回用户id
        mapper.insert(user);
        Integer uid=user.getUid();
        return BlogResult.build(200,"用户添加成功",uid);
    }
    @Override
    public BlogResult findUserById(Integer id) {
        SysUser user=mapper.selectByPrimaryKey(id);
        BlogResult result=null;
        if(user==null)
        {
            result=BlogResult.build(404,"不存在改用户");
        }
        else
        {
            result=BlogResult.build(200,"用户存在",user);
        }
        return result;
    }

    @Override
    public BlogResult findUserByName(String name) {
        SysUserExample example=new SysUserExample();
        SysUserExample.Criteria  criteria=example.createCriteria();
        criteria.andUsernameEqualTo(name);
        List<SysUser> users=mapper.selectByExample(example);
        BlogResult result=null;
        if(users!=null&&users.size()==1)
        {
            result=BlogResult.build(200,"用户已经存在",users.get(0));
        }else
        {
            result=BlogResult.build(404,"不存在改用户");
        }
        return result;

    }
    @Override
    public BlogResult updateUser(SysUser user) {
        mapper.updateByPrimaryKey(user);
        return BlogResult.ok();
    }
}
